<?php


namespace App\Http\Controllers;
use App\Models\Label;
use Illuminate\Http\Request;

class title_admin extends \Illuminate\Routing\Controller
{
    public function title_admin(Request $request)
    {
        $m=5;
        $page=$request->input('page');
        $page=isset($page)?$page:1;
        $n=($page-1)*$m;
        $label=new label();
        $search=$request->input('title');
        $labelRow = $label->Where('name','like','%'.$search.'%')->skip($n)->take($m)->get();
        $count=$label->Where('name','like','%'.$search.'%')->get()->count('id');
        return view('title_admin',['labelRow'=>$labelRow,'m'=>$m,'count'=>$count,'page'=>$page,'search'=>$search]);
    }
    public function title_admin_del(Request $request)
    {
        $label=new label();
        $labelRow=$request->input('id');
        $labelRows=$label->where('id',$labelRow)->delete();
        if($labelRows) {
            echo '删除成功'.'<a href="/title_admin">点击回到标签页</a>';
        }else{
            echo '失败';
        }
    }
    public function title_admin_up(Request $request)
    {
        $label=new label();
        $id=$request->input('id');
        $labelRows=$label->where('id',$id)->first();
        $labelRow=$label->get();
        return view('title_admin_up',['labelRows'=>$labelRows,'labelRow'=>$labelRow]);
    }
    public function title_admin_up_ok(Request $request)
    {
        $name=$request->input('name');
        $style=$request->input('style');
        $id=$request->input('id');
        $label=new label();
        $up=$label->where('id',$id)->update(['name'=>$name,'style'=>$style]);
        if($up){
            echo '修改成功'.'<a href="/title_admin">点击回到标签页</a>';
        }else{
            echo '修改失败';
        }
    }
    public function title_admin_add(Request $request)
    {
        return view('title_admin_add');
    }
    public function title_admin_add_ok(Request $request)
    {
        $name=$request->input('name');
        $style=$request->input('style');
        $label=new label();
        $labelRows=$label->insert(['name'=>$name,'style'=>$style]);
        if($labelRows=="") {
            echo '不能添加空值请重试';
        }else{
            echo '添加成功'.'<a href="/title_admin">点我回主页</a>';
        }
    }
}
